﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;

namespace QuanLyKTX
{
    using DAL;
    public partial class frm_SortRoomPopup : Telerik.WinControls.UI.RadForm
    {

        public int IDSinhVien { get; set; }

        #region Private Methods
        private void DefaultControl()
        {
            radListControl_SinhVien.ListElement.ItemHeight = 28;            
            radDropDownList_SoPhong.ListElement.ItemHeight = 28;
        }

        private void BindPhongTrong()
        {
            using (var db = new QuanLyKTXDataContext())
            {
                // Lay gioi tinh cua SV
                var gioiTinh = (from p in db.tbl_SinhViens
                                where p.ID == IDSinhVien
                                select p.GioiTinh).FirstOrDefault();
                if (gioiTinh != null)
                {
                    // Lay danh sach phong con trong cho phu hop voi gioi tinh
                    var results = from p in db.tbl_Phongs
                                  where p.LoaiPhong == gioiTinh && p.SoGiuongTrong > 0
                                  select p;

                    if (results != null)
                    {
                        radDropDownList_SoPhong.ValueMember = "SoPhong";
                        radDropDownList_SoPhong.DisplayMember = "SoPhong";
                        radDropDownList_SoPhong.DataSource = results;
                    }
                }
            }
        }

        private void BindThongTinPhong(string soPhong)
        {
            if (!String.IsNullOrEmpty(soPhong))
            {
                using (var db = new QuanLyKTXDataContext())
                {
                    var thongTinPhong = (from p in db.tbl_Phongs
                                         where p.SoPhong == soPhong
                                         select new
                                         {
                                             LoaiPhong = p.LoaiPhong.Value ? "Nam" : "Nữ",
                                             p.SoGiuongTrong,
                                         }).FirstOrDefault();

                    var sinhVienHienCo = from p in db.tbl_XepPhongs
                                         join q in db.tbl_SinhViens on p.ID equals q.ID
                                         where p.SoPhong == soPhong
                                         select new
                                         {
                                             HoTen = String.Format("{0} {1}", q.Ho, q.Ten)
                                         };

                    radLabel_LoaiPhong.Text = String.Format("Loại phòng: {0}", thongTinPhong.LoaiPhong);
                    radLabel_ConTrong.Text = String.Format("Còn trống: {0} chỗ", thongTinPhong.SoGiuongTrong);

                    radListControl_SinhVien.DisplayMember = "HoTen";
                    radListControl_SinhVien.ValueMember = "HoTen";
                    radListControl_SinhVien.DataSource = sinhVienHienCo;
                }
            }
        }

        private bool InsXepPhong(string soPhong, int id, DateTime ngayVao)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                if (db.tbl_Phongs.Where(p => p.SoPhong == soPhong && p.SoGiuongTrong > 0).Count() > 0)
                {
                    try
                    {
                        tbl_XepPhong tblXepPhong = new tbl_XepPhong
                        {
                            SoPhong = soPhong,
                            ID = id,
                            NgayVao = ngayVao
                        };

                        db.tbl_XepPhongs.InsertOnSubmit(tblXepPhong);

                        // Update Chỗ trống trong phòng
                        var resultPhong = (from p in db.tbl_Phongs
                                           where p.SoPhong == soPhong && p.SoGiuongTrong > Convert.ToByte(0)
                                           select p).FirstOrDefault();
                        int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) - 1;

                        resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);
                        db.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return false;
                    }
                }
                return false;
            }
        }

        #endregion

        #region Event

        private void radButton_Luu_Click(object sender, EventArgs e)
        {
            string soPhong = radDropDownList_SoPhong.SelectedValue.ToString();
            DateTime ngayVao = radDateTimePicker_NgayVao.Value;

            if (InsXepPhong(soPhong, IDSinhVien, ngayVao))
            {
                Close();                
            }
            else
            {
                MessageBox.Show("Chưa thực hiện việc xếp phòng");
            }
        }

        private void radButton_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void radDropDownList_SoPhong_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            BindThongTinPhong(radDropDownList_SoPhong.SelectedValue.ToString());
        }

        private void frm_SortRoomPopup_Load(object sender, EventArgs e)
        {
            DefaultControl();
            BindPhongTrong();
            BindThongTinPhong(radDropDownList_SoPhong.SelectedValue.ToString());
        }

        public frm_SortRoomPopup(int idSinhVien)
        {
            InitializeComponent();
            IDSinhVien = idSinhVien;
        }        
        #endregion
    }
}
